<?php

class Upload {
	
	function do_upload($field, $dir) {
		$file_name = $_FILES[$field]['name'];
		$extend = pathinfo($file_name); 
		$extend = strtolower($extend["extension"]);
		
		$rand_file_name = rand(1,100000) . "." . $extend;
		$target_path = ABSPATH . $dir . '/';
		$this->mkdirs( $target_path );
		$target_path .= $rand_file_name;
		
   		if(@move_uploaded_file($_FILES[$field]['tmp_name'], $target_path)) {
   			return $dir . '/' . $rand_file_name;
   		}
   		else {
   			echo "upload error";
   			exit;
   		}
	}
	
	function mkdirs($dir) {
		if( ! is_dir( $dir ) )	{  
			if( ! $this->mkdirs( dirname($dir) ) ) {  
				return false;  
			}  
			if( ! mkdir($dir,0777) ) {  
				return false;  
			}  
		}  
		return true;
	}
}

?>